﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using Dln.Common;
using Dln.Common.WxPayAPI;
using System.Web.SessionState;
using Dln.Common.Helper;

namespace Dln.Handler
{
    public class TokenBussiness : System.Web.UI.Page, IRequiresSessionState
    {
        public static object GetTokenKey()
        {

            var token = HttpContext.Current.Session["Dln_access_token"];

            if (token == null || token == "")
            {
                token = GetTokenKeyByUrl();
                if (token != null && token != "")
                {
                    HttpContext.Current.Session["Dln_access_token"] = token;
                    HttpContext.Current.Session.Timeout = 10;
                }
            }
            return token;
        }

        public static string GetTokenKeyByUrl()
        {
            try
            {
                //string strUrl = CommonHelper.GetAppSettings("GetTokenUrl");
                //HttpWebRequest wrequest = (HttpWebRequest)WebRequest.Create(strUrl);
                //wrequest.UseDefaultCredentials = false;
                //WebResponse response = wrequest.GetResponse();
                //Stream resStream = response.GetResponseStream();
                //StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
                //string strTokenKey = sr.ReadToEnd();
                //resStream.Close();
                return WeiXinConfig.AccessToken;
            }
            catch (Exception ex)
            {
                //todo
                return "";
            }
        }
    }
}
